// components/home-users/home-users.ts
import tran from '../../../utils/language';
import { userList, userTargetInfo } from '../../../utils/api.js'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    _t: tran._t('friendResult'),
    list: [
      {
        img: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
        mbit: '/images/mbti.png',
        name: 'weixin',
        year: '25'
      },
      {
        img: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
        mbit: '/images/mbti.png',
        name: 'weixin',
        year: '25'
      },
      {
        img: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
        mbit: '/images/mbti.png',
        name: 'weixin',
        year: '25'
      },
      {
        img: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
        mbit: '/images/mbti.png',
        name: 'weixin',
        year: '25'
      },
      {
        img: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
        mbit: '/images/mbti.png',
        name: 'weixin',
        year: '25'
      },
      {
        img: 'https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg',
        mbit: '/images/mbti.png',
        name: 'weixin',
        year: '25'
      }
    ],
    selectBtnList: [
      {
        selectFlag: true,
        text: '性格'
      },
      {
        selectFlag: false,
        text: '年龄'
      },
      {
        selectFlag: false,
        text: '区域'
      },
      {
        selectFlag: false,
        text: '性别'
      }
    ],
    downPopupParams: {
      showDownPopup: false,
      showType: 1 // 0 注册 1 今天想做什么
    },
    searchParams: {
      interestIds: '',
      characterType: '',
      professionId: null,
      personalities: '',
      professionName: null,
      interestName: '',
      languageText: '',
      languages: null,
      genderText: '',
      gender: '',
      ageText: '',
      minAge: null,
      maxAge: null,
      online: 'all',
      currentProvinceCode: '',
      currentCityCode: '',
      currentDistrictCode: '',
      hometownProvinceCode: '',
      hometownCityCode: '',
      hometownDistrictCode: '',
      friend: false,
      requestPlayIds: '',
    },
    filterParams: {
      interestIds: '',
      characterType: '',
      professionId: null,
      personalities: '',
      professionName: null,
      interestName: '',
      languageText: '',
      languages: null,
      genderText: '',
      gender: '',
      ageText: '',
      minAge: null,
      maxAge: null,
      online: 'all',
      currentProvinceCode: '',
      currentCityCode: '',
      currentDistrictCode: '',
      hometownProvinceCode: '',
      hometownCityCode: '',
      hometownDistrictCode: '',
      friend: false,
      requestPlayIds: '',
      requestHelpIds: '',
      requestTalkIds: '',
      regionTextList: [],
      ageValue: ''
    },
    mbtiSearch: true,
    ageSearch: true,
    regionSearch: true,
    genderSearch: true,
    userArrList: [],
    firstSearchFlag: false,
    homeFriendInfo: {},
    jumpType: 1, // 1找搭子一起玩  2找师傅学点啥  3找朋友聊聊天
    filterPopupParams: {
      showDownPopup: false,
      showType: 1 // 0 注册 1 今天想做什么
    },
    headerTitle: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    let headerTitle = ''
    if (options.type == '1') { // 找搭子
      headerTitle = this.data._t['headerTitle1']
    } else if (options.type == '2') { // 找师傅
      headerTitle = this.data._t['headerTitle2']
    } else { // 找朋友
      headerTitle = this.data._t['headerTitle3']
    }
    if (options.info) {
      let { filterParams } = this.data
      const info = JSON.parse(options.info)
      const obj = { ...filterParams, ...info }
      console.log(obj,'这');
      this.setData({
        filterParams: obj,
        jumpType: options.type,
        headerTitle
      }, () => {
        this.getUserList()
      })
    }

  },
  /**
   * 点击筛选的icon,弹出筛选弹框
   */
  filterShowPopup() {
    let { filterPopupParams, filterParams } = this.data
    filterPopupParams.showDownPopup = true
    this.setData({
      filterPopupParams
    })
  },
  filterSelectChange (e: any) {
    let { filterParams } = this.data
    const filterObj = e.detail
    filterParams.personalities = filterObj.personalities
    filterParams.currentProvinceCode = filterObj.currentProvinceCode || filterObj.provincesValue
    filterParams.currentCityCode = filterObj.currentCityCode || filterObj.cityValue
    filterParams.currentDistrictCode = filterObj.currentDistrictCode || filterObj.regionValue
    filterParams.minAge = filterObj.minAge || null
    filterParams.maxAge = filterObj.maxAge || null
    filterParams.genderText = filterObj.genderText || null
    filterParams.gender = filterObj.gender || null
    filterParams.regionTextList = filterObj.regionTextList
    filterParams.ageText = filterObj.ageText
    filterParams.ageValue = filterObj.ageValue
    this.setData({
      filterParams
    }, () => {
      this.getUserList()
    })
  },
  typeFilterChange(e: any) {
    const list = e.detail
    list.length > 0 && list.map((item: any) => {
      if (item.value == 'mbti') {
        this.setData({
          mbtiSearch: item.selectFlag
        })
      }
      if (item.value == 'age') {
        this.setData({
          ageSearch: item.selectFlag
        })
      }
      if (item.value == 'region') {
        this.setData({
          regionSearch: item.selectFlag
        })
      }
      if (item.value == 'gender') {
        this.setData({
          genderSearch: item.selectFlag
        })
      }
    })
    setTimeout(() => {
      this.getUserList()
    }, 100)
  },
  /**
   * 获取用户列表方法
   */
  getUserList() {
    const { mbtiSearch, ageSearch, regionSearch, genderSearch, filterParams } = this.data
    const { personalities, interestIds, professionId, languages, currentProvinceCode, currentCityCode, currentDistrictCode, gender, minAge, maxAge, online, hometownProvinceCode, hometownCityCode, hometownDistrictCode, friend, requestPlayIds, requestHelpIds, requestTalkIds } = filterParams
    // const { hometownProvinceCode, hometownCityCode, hometownDistrictCode, friend, requestPlayIds } = searchParams
    // 不选或者选择全部就传null
    let languagesValue = null;
    if (languages) {
      if (languages == 'all') { // 全部
        languagesValue = null
      } else {
        languagesValue = [languages] // 传数组
      }
    }
    let params = {
      pageNum: 1,
      pageSize: 100,
      personalities: personalities ? personalities.split(',') : null, // 性格类型,传数组
      interestIds: interestIds ? interestIds.split(',') : null, // 兴趣
      professionId: professionId, // 职业技能
      languages: languagesValue, // 语言,全部就是传null
      currentProvinceCode, // 省份code
      currentCityCode, // 市的code
      currentDistrictCode, // 区的code
      userGender:gender == '3' ? null : gender, // 性别,如果是3,就是选择的全部
      minAge: minAge || null, // 年龄最小值
      maxAge: maxAge || null, // 年龄最大值
      hometownProvinceCode,
      hometownCityCode,
      hometownDistrictCode,
      friend,
      requestPlayIds: requestPlayIds ? [requestPlayIds] : [],
      requestHelpIds: requestHelpIds ? [requestHelpIds] : [],
      requestTalkIds: requestTalkIds ? [requestTalkIds] : [],
      online: online == 'all' ? null : true // 在线状态,全部就是传null
    }

    // if (!mbtiSearch) {
    //   params.personalities = null
    // }
    // if (!ageSearch) {
    //   params.minAge = null
    //   params.maxAge = null
    // }
    // if (!regionSearch) {
    //   params.currentProvinceCode = ''
    //   params.currentCityCode = ''
    //   params.currentDistrictCode = ''
    //   params.hometownCityCode = ''
    //   params.hometownDistrictCode = ''
    //   params.hometownProvinceCode = ''
    // }
    // if (!genderSearch) {
    //   params.personalities = null
    // }
   
    userList(params).then(res => {
      if (res.code == 7000) {
        this.setData({
          hasMobile: false
        })
      } else {
        const list = res.records
        this.setData({
          userArrList: list,
          userArrObj: res.data,
          hasMobile: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  /**
   * 用户筛选框确认按钮
   * @param e 
   */
  filterConfirm(e: any) {
    const obj = e.detail
    const { filterParams } = this.data
    filterParams.interestIds = obj.interestIds || filterParams.interestIds
    filterParams.characterType = obj.characterType || filterParams.characterType
    filterParams.professionId = obj.professionId || filterParams.professionId
    filterParams.personalities = obj.personalities || filterParams.personalities
    filterParams.professionName = obj.professionName || filterParams.professionName
    filterParams.interestName = obj.interestName || filterParams.interestName
    filterParams.languageText = obj.languageText || filterParams.languageText
    filterParams.languages = obj.languages || filterParams.languages
    filterParams.genderText = obj.genderText || filterParams.genderText
    filterParams.gender = obj.gender || filterParams.gender
    filterParams.ageText = obj.ageText || filterParams.ageText
    filterParams.minAge = obj.minAge || filterParams.minAge
    filterParams.maxAge = obj.maxAge || filterParams.maxAge
    filterParams.online = obj.online || filterParams.online
    filterParams.currentProvinceCode = obj.currentProvinceCode || filterParams.currentProvinceCode
    filterParams.currentCityCode = obj.currentCityCode || filterParams.currentCityCode
    filterParams.currentDistrictCode = obj.currentDistrictCode || filterParams.currentDistrictCode
    filterParams.hometownProvinceCode = obj.hometownProvinceCode || filterParams.hometownProvinceCode
    filterParams.hometownCityCode = obj.hometownCityCode || filterParams.hometownCityCode
    filterParams.hometownDistrictCode = obj.hometownDistrictCode || filterParams.hometownDistrictCode
    this.setData({
      filterParams: filterParams
    }, () => {
      // 关系弹框
      this.closeFilterDownPopup()
      // 查询用户列表
      this.getUserList()
    })
  },
  closeFilterDownPopup() {
    const { filterPopupParams } = this.data
    filterPopupParams.showDownPopup = false
    this.setData({ filterPopupParams: filterPopupParams })
  },
  handleUser(e: any) {
    // 获取子组件传过来的数据
    const info = e.detail
    this.getOtherUserInfo(info)
  },
  /**
   * 获取点击的用户详情
   */
  getOtherUserInfo(info: any) {
    const { jumpType, filterParams } = this.data
    const params = {
      targetUserId: info.userId
    }
    userTargetInfo(params).then(res => {
      const { downPopupParams } = this.data
      downPopupParams.showDownPopup = true
      res.jumpType = jumpType // 1找搭子  2找师傅  3找朋友
      res.requestPlayIds = filterParams.requestPlayIds
      res.requestHelpIds = filterParams.requestHelpIds
      res.requestTalkIds = filterParams.requestTalkIds
      this.setData({ downPopupParams: downPopupParams, homeFriendInfo: res })
    })
  },
  closeDownPopup() {
    // const downPopup = this.selectComponent('#down-popup');
    const { downPopupParams } = this.data
    downPopupParams.showDownPopup = false
    this.setData({ downPopupParams: downPopupParams })
    // downPopup && downPopup.hidePopup()
  },
  changeSelect(e: any) {
    const { info } = e.currentTarget.dataset
    const { selectBtnList } = this.data
    // 判断用户点击的是哪一个按钮，从而将那一个按钮状态反选
    selectBtnList.map(item => {
      if (item.text === info.text) {
        item.selectFlag = !item.selectFlag
      }
    })
    this.setData({
      selectBtnList: selectBtnList
    })
  },
  clickUser(e: any) {
    const { info } = e.currentTarget.dataset
    // 给父组件传值
    this.triggerEvent('userEvent', info);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
